package com.red.wood.util;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.spark.api.java.JavaRDD;

import com.red.wood.model.SqlInfo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class CommonUtils {
	
	private static final Log log = LogFactory.getLog(CommonUtils.class);
	
	public static String valueOf(Object object){
		String str = StringUtils.trim(String.valueOf(object));
		boolean check = StringUtils.isBlank(str) || StringUtils.equals(str, "null") || StringUtils.equals(str, "undefined");
		return check ? "" : str;
	}
	
	public static String filteNull(Object object) {
		String str = valueOf(object);
		boolean check = StringUtils.isBlank(str);
		return check ? "0" : str;
	}
	
	/**
	 * 获取配置文件中的信息
	 * @return
	 */
	public static SqlInfo getSqlInfo() {
		SqlInfo sql = null;
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlInfo.class);
			ClassLoader classLoader = CommonUtils.class.getClassLoader();  
			String xml = IOUtils.toString(classLoader.getResourceAsStream("sqlconfig/sqlInfo.xml"),"UTF-8"); 
			sql = (SqlInfo) xstream.fromXML(xml);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return sql;
	}
	
	/**
	 * 特殊字符校验，防扫描
	 * @param list
	 */
	public static void fieldCheck(List<String[]> list){
		for(String[] str : list){
			if(str.length < 2){
				continue;
			}
			if(str[0].indexOf("'") >= 0 || str[0].indexOf("\"") >= 0){
				throw new RuntimeException("【"+str[1]+"】不能包含单引号和双引号！");
			}
		}
	}
	
	/**
	 * IP校验
	 * @param ip
	 * @return
	 */
	public static boolean isValidIp(String ip){
		String reg = "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\."
				+ "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\."
				+ "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\."
				+ "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$";
		Pattern pattern = Pattern.compile(reg);
		Matcher p = pattern.matcher(ip);
		return p.matches();
	}
	
	  // 判断输入的IP是否合法
    public static boolean isIp(String str) {
        if (str != null) {
            Pattern pattern = Pattern.compile("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$");
            return pattern.matcher(str).matches();
        } else {
            return false;
        }
    }
    
    public static void uploadHdfs(JavaRDD<String> rdd,String dwdPath) {
		try{
			FileSystem fileSystem = FileSystem.get(new Configuration()); 
			Path hdfsPath = new Path(dwdPath);
			boolean flag = fileSystem.isDirectory(hdfsPath);
			log.info("hdfspath=="+dwdPath);
			log.info("flag=="+flag);
			if(flag){
				fileSystem.delete(hdfsPath,true);
			}
			rdd.saveAsTextFile(dwdPath);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
	}
    
	public static boolean isValidDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try{
			sdf.setLenient(false);//严格按照给定的格式来校验
			sdf.parse(date);
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 转换为Integer类型
	 */
	public static Integer toInteger(Object val){
		return toLong(val).intValue();
	}
	
	/**
	 * 转换为Long类型
	 * @param val
	 * @return
	 */
	public static Long toLong(Object val){
		return toDouble(val).longValue();
	}
	
	/**
	 * 转换为Double类型
	 * @param val
	 * @return
	 */
	public static Double toDouble(Object val){
		if (val == null){
			return -999D;
		}
		try {
			return Double.valueOf(valueOf(val));
		} catch (Exception e) {
			return -999D;
		}
	}
}